package com.kongzue.baseokhttpv3;

import com.kongzue.baseokhttp.BaseWebSocket;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.Logger;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.listener.OnDownloadListener;
import com.kongzue.baseokhttp.listener.WebSocketStatusListener;
import com.kongzue.baseokhttp.util.*;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.hiviewdfx.HiLog;
import okhttp3.Response;
import okio.ByteString;
import org.junit.Test;

import java.io.File;
import java.net.SocketException;

public class ExampleOhosTest {
    @Test
    public void Setting() {
        BaseOkHttpConfig.getInstance().setReserveServiceUrls(new String[]{"https://www.twitter.com", "https://api.apiopen.top"});
        BaseOkHttpConfig.getInstance().setServiceUrl("https://www.fastmock.site");
    }

    @Test
    public void POST() {
        Setting();
        HttpRequest.POST(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/getWangYiNews",
                new Parameter().add("page", "1").add("count", 5), new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {

                        if (error == null) {
                            HiLog.info(Logger.LABEL,
                                    "请求成功" + main.getList("result").toString());

                            JsonList dataList = main.getList("result");
                            HiLog.info(Logger.LABEL, dataList.toString());
                        } else {
                            HiLog.info(Logger.LABEL, "请求超时".toString());
                        }
                    }
                });
    }

    @Test
    public void POST_SSL() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName("test.cer");
        HttpRequest.POST(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/getWangYiNews",
                new Parameter().add("page", "1").add("count", 5), new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                        if (error == null) {
                            HiLog.info(Logger.LABEL,
                                    "请求成功" + main.getList("result").toString());
                            JsonList dataList = main.getList("result");
                        } else {

                            HiLog.info(Logger.LABEL, "请求超时".toString());
                        }
                    }
                });
    }

    @Test
    public void doDownload() {
        String url = "https://xiazai.xiazaiba.com/Soft/c/com.ivali.qhbao.apK";
        HttpRequest httpRequest = HttpRequest.build(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(),
                url);
        String filePath = url.trim();
        httpRequest.doDownload(
                new File(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext().getExternalFilesDir("ceshi"), filePath.substring(url.trim().lastIndexOf("/"))),
                new OnDownloadListener() {
                    @Override
                    public boolean onDownLoadCheck(Response response) {
                        long total = response.body().contentLength();
                        //文件下载失败
                        if (total < 0 || response.code() != 200) {

                            return false;
                        }
                        return true;
                    }

                    @Override
                    public void onDownloadSuccess(File file) {
                        HiLog.info(Logger.LABEL, "文件已下载完成");
                    }

                    @Override
                    public void onDownloading(int progress) {
                        HiLog.info(Logger.LABEL, "进度" + progress);
                    }

                    @Override
                    public void onDownloadFailed(Exception e) {
                        if (e instanceof SocketException) {
                            HiLog.info(Logger.LABEL, "下载取消" + e.toString());
                        } else {
                            HiLog.info(Logger.LABEL, "下载失败" + e.toString());
                        }
                    }
                });
    }

    @Test
    public void baseWebSocket() {
        BaseWebSocket baseWebSocket = BaseWebSocket.BUILD(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(),
                "wss://echo.websocket.org").setWebSocketStatusListener(
                new WebSocketStatusListener() {

                    @Override
                    public void connected(Response response) {
                    }

                    @Override
                    public void onMessage(String message) {
                    }

                    @Override
                    public void onMessage(ByteString message) {

                    }


                    @Override
                    public void onReconnect() {
                    }

                    @Override
                    public void onDisconnected(int breakStatus) {
                    }

                    @Override
                    public void onConnectionFailed(Throwable t) {
                    }
                });
        baseWebSocket.startConnect();
    }

    @Test
    public void deletejson() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter header = new Parameter();
        HttpRequest.build(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/mock/d2ac8d0e4404412de7b2b4e23f026c63/_ceshi-01/delete/test").setJsonParameter("{\n" +
                "    \"key\": \"177c070d1922f938ccee95b6bd04df0b\",\n" +
                "    \"city\": \"上海市\"\n" +
                "}").setHeaders(header).setJsonResponseListener(new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {

            }
        }).doDelete();
    }

    @Test
    public void postbiaodan() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter parameter = new Parameter();

        HttpRequest.POST(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/mock/5009ac87331ccbc9fce98559554f58a5/test/test_postform",
                parameter, new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                    }
                });

    }

    @Test
    public void postjson() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter parameter = new Parameter();
        HttpRequest.JSONPOST(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/mock/5009ac87331ccbc9fce98559554f58a5/test/test_postjson",
                "{\n" +
                        "    \"key\": \"177c070d1922f938ccee95b6bd04df0b\",\n" +
                        "    \"city\": \"上海市\"\n" +
                        "}", new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                    }
                });
    }

    @Test
    public void put() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter parameter = new Parameter();
        Parameter header = new Parameter();
        HttpRequest.PUT(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/mock/5009ac87331ccbc9fce98559554f58a5/test/test/put", header,
                parameter, new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                    }
                });

    }

    @Test
    public void getbiaodan() {
        Setting();
        BaseOkHttpConfig.getInstance().setSSLInAssetsFileName(null);
        Parameter parameter = new Parameter();
        HttpRequest.GET(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext(), "/mock/5009ac87331ccbc9fce98559554f58a5/test/get_api",
                parameter, new JsonResponseListener() {
                    @Override
                    public void onResponse(JsonMap main, Exception error) {
                    }
                });

    }
}